const webpack = require("webpack");
const path = require("path");
/**
 *
 * @param {Pick<import("webpack").Configuration,"entry" | "output">} config
 */
function WebpackBuildTool(config) {
  console.log(config.entry);
  return new Promise((resolve, reject) => {
    const compiler = webpack(
      {
        entry: config.entry,
        mode: "production",
        experiments: {
          outputModule: true,
        },
        devtool: false,
        output: {
          library: { type: "module" },
          globalObject: "this",
          ...config.output,
        },
        externalsType: "module",
        externals: [
          {
            // ohos_util: "@ohos.util",
            // ohos_process: "@ohos.process",
            // ohos_hilog: "@ohos.hilog",
            buffer: "buffer",
            process: "process",
            events: "events",
            string_decoder: "string_decoder",
          },
          /^@ohos\..+/,
        ],

        // externals: [
        //   "@ohos.util",
        //   "@ohos.process",
        //   "@ohos.hilog",
        //   "@ohos.util",
        //   "buffer",
        //   "process",
        //   "events",
        //   "string_decoder",
        // ],
        resolve: {
          extensions: [".js", ".ts", ".esm.js"],
        },
      },
      (err) => {
        if (err) {
          reject(err);
        } else {
          resolve(true);
          console.log("编译完成");
        }
      }
    );
    console.log("compiler执行完成");
    compiler.run((err, stats) => {
      if (err) {
        reject(err);
      } else {
        console.log("compiler.run回调完成");
      }
    });
    console.log("compiler.run执行完成");
  });
}

module.exports = WebpackBuildTool;
